//
//  TaskViewController.m
//  tcpoctest
//
//  Created by gump on 6/14/13.
//  Copyright (c) 2013 gump. All rights reserved.
//

#import "TaskViewController.h"
#import "taskDataSource.h"
#import "taskCell.h"
#import "TaskInfo.h"

@implementation TaskViewController

-(void)initUI
{
    self.view.backgroundColor = VIEW_BG_COLOR;
    
    [self createTitle];
    self.mtitle.text = @"任务";
    
    UIBarButtonItem *backItem = [self createBarBackButtonItemWithTitle:@"返回"
                                                                target:self
                                                                action:@selector(backButtonPressed)];
    self.navItem.leftBarButtonItem = backItem;
    
    mtable =[[UITableView alloc]initWithFrame:CGRectMake(0, TOPBAR_HIGHT, WINDOW_WIDTH, WINDOW_HIGTH - TOPBAR_HIGHT - 20)
                                        style:UITableViewStylePlain];
    mtable.backgroundColor=CELL_BG_COLOR;
    mtable.separatorStyle=UITableViewCellSeparatorStyleSingleLine;
    mtable.delegate =self;
    mtable.dataSource =self;
    [self.view insertSubview:mtable belowSubview:self.mtitle];
}

#pragma mark -private fun-
-(void)backButtonPressed
{
    [[ViewManager sharedViewManager] backView:self];
}

-(void)viewWillShow
{
    [mtable reloadData];
}

#pragma mark - tableView DataSource -
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section==0)
    {
        if ([DataControl shareControl].taskDS != nil) {
            
            return [[DataControl shareControl].taskDS GetShowCount];
        }
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSUInteger section=[indexPath section];
    NSUInteger row = [indexPath row];
    
    static NSString *CellIdentifier = @"Cell";
    taskCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[taskCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] ;
		cell.selectionStyle = UITableViewCellSelectionStyleGray;
    }
    
    cell.accessoryType = UITableViewCellAccessoryNone;
    
    cell.imageView.image = nil;
    
    switch (section)
    {
        case 0:
        {
            TaskInfo *tinfo = [[DataControl shareControl].taskDS getInfoByRow:row];
            
            if (tinfo != nil) {
                switch (tinfo.itaskState) {
                    case TASK_STATE_GET:
                    {
                        cell.completeLabel.backgroundColor = [UIColor greenColor];
                        cell.completeLabel.text = @"已领取";
                        cell.detailTextLabel.textColor = [UIColor redColor];
                    }
                        break;
                    case TASK_STATE_NOGET:
                    {
                        cell.completeLabel.backgroundColor = [UIColor grayColor];
                        cell.completeLabel.text = @"未领取";
                        cell.detailTextLabel.textColor = [UIColor grayColor];
                    }
                        break;
                    default:
                        break;
                }
                
            cell.textLabel.text = tinfo.taskname;
            cell.detailTextLabel.text = tinfo.strDesc;
            }
            
            
        }
            cell.selectionStyle=UITableViewCellSelectionStyleNone;
            break;
        default:
            break;
    }
    
    return cell;
}

#pragma mark -table delegate-
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    switch ([indexPath section]) {
        case 0:
        {
        }
            break;
            
        default:
            break;
    }
}
@end
